/**
 * Create tao on 2019/5/17
 */
package com.cole.miniapp.project.service.impl;

import com.cole.miniapp.project.domain.*;
import com.cole.miniapp.project.domain.vo.FcmBusinessVO;
import com.cole.miniapp.project.domain.vo.FcmStoreVO;
import com.cole.miniapp.project.enumeration.store.StoreStateEmun;
import com.cole.miniapp.project.enumeration.user.UserIdentityEmun;
import com.cole.miniapp.project.mapper.*;
import com.cole.miniapp.project.service.StoreService;
import com.cole.miniapp.project.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


@Service
public class StoreServiceImpl implements StoreService {

    @Autowired
    private FcmStoreMapper fcmStoreMapper;

    @Autowired
    private FcmBusinessMapper fcmBusinessMapper;

    @Autowired
    private FcmFractionProportionMapper fcmFractionProportionMapper;

    @Autowired
    private FcmUserFractionMapper  fcmUserFractionMapper;

    @Autowired
    private FcmUserConsumptionMapper fcmUserConsumptionMapper;

    @Autowired
    private FcmUserRechargesMapper fcmUserRechargesMapper;

    @Autowired
    private FcmStoreInfoMapper fcmStoreInfoMapper;

    @Autowired
    private FcmUserMapper fcmUserMapper;

    @Value("${show.row}")
    private int showRow;

    /**
     * 展示根据经纬度和营业状态展示商家
     *
     * @param page
     * @param search
     * @return
     */

    @Override
    public List<FcmStoreVO> getStoreList(int page,String search) {
        List<FcmStoreVO> list;
        // 显示全部商家
        if (search == null) {
            Parameter2 parameter2 = new Parameter2();
            parameter2.setVar1(page * showRow);
            parameter2.setVar2(showRow);
            list = fcmStoreMapper.selectAllStore(parameter2);
        } else {
            // 分别显示营业商家和非营业商家
            Parameter3 parameter3 = new Parameter3();
            parameter3.setVar1("%"+search+"%");
            parameter3.setVar2(page * showRow);
            parameter3.setVar3(showRow);
            list = fcmStoreMapper.selectAllStoreBySearch(parameter3);
        }
        return list;
    }

    /**
     * 商家入口
     *
     * @param userId
     * @param page
     * @return
     */

    @Override
    public List<FcmStore> storeEntrance(long userId, int page) {
        Parameter3 parameter3 = new Parameter3();
        parameter3.setVar1(userId);
        parameter3.setVar2(page * showRow);
        parameter3.setVar3(showRow);
        List<FcmStore> list = fcmStoreMapper.selectUserStoreByUserId(parameter3);
        return list;
    }

    /**
     * 老板设置积分赠送比例
     *
     * @param fcmBusiness
     * @param proportion
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean setFractionProportion(FcmBusiness fcmBusiness, BigDecimal proportion) {
        FcmFractionProportion fcmFractionProportion = fcmFractionProportionMapper.selectFractionProportionByStoreIdAndZero(fcmBusiness.getStoreId());
        if (fcmFractionProportion == null) {
            FcmFractionProportion fcmFractionProportion2=new FcmFractionProportion();
            fcmFractionProportion2.setStoreId(fcmBusiness.getStoreId());
            fcmFractionProportion2.setGoodsId((byte) 0);
            fcmFractionProportion2.setUserId(fcmBusiness.getUserId());
            fcmFractionProportion2.setFractionProportion(proportion);
            fcmFractionProportion2.setCreateTime(new Date());
            fcmFractionProportionMapper.insert(fcmFractionProportion2);
            return true;
        } else {
            fcmFractionProportion.setUserId(fcmBusiness.getUserId());
            fcmFractionProportion.setFractionProportion(proportion);
            fcmFractionProportion.setCreateTime(new Date());
            fcmFractionProportionMapper.updateByPrimaryKey(fcmFractionProportion);
            return false;
        }
    }

    /**
     * 添加员工/店长
     *
     * @param type
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addStaff(FcmBusiness fcmBusiness, Long storeUserId, String name, String phone, Byte type) {
        FcmBusinessKey fcmBusinessKey = new FcmBusinessKey();
        fcmBusinessKey.setStoreId(fcmBusiness.getStoreId());
        fcmBusinessKey.setUserId(storeUserId);

        //判断用户是否存在，如果存在手机号是否正确
        FcmUser fcmUser = fcmUserMapper.selectByPrimaryKey(storeUserId);
        if (fcmUser == null) {
            return 1;
        }
        if (!fcmUser.getPhone().equals(phone)) {
            return 2;
        }

        //判断添加人是否有权限添加
        FcmBusiness fcmBusiness2 = fcmBusinessMapper.selectByPrimaryKey(fcmBusinessKey);
        if (fcmBusiness.getType()<type){
            //判断用户是否要添加的员工是否存在
            if (fcmBusiness2==null){
                fcmBusiness.setUserId(storeUserId);
                fcmBusiness.setName(name);
                fcmBusiness.setType(type);
                fcmBusiness.setCreateTime(new Date());
                fcmBusinessMapper.insert(fcmBusiness);
                //如果用户身份为普通用户，更新用户为商家
                if (fcmUser.getType()==0){
                    fcmUser.setType(UserIdentityEmun.CODE_1.getCode());
                    fcmUserMapper.updateTypeByUserId(fcmUser);
                }
                return 3;
            }
            return 4;
        }
        return 5;
    }

    /**
     * 老板/店长删除店员/店长
     *
     * @param fcmBusiness
     * @param storeUserId
     * @return
     */
    @Override
    public Integer deleteStaff(FcmBusiness fcmBusiness, Long storeUserId) {
        FcmBusinessKey fcmBusinessKey = new FcmBusinessKey();
        fcmBusinessKey.setStoreId(fcmBusiness.getStoreId());
        fcmBusinessKey.setUserId(storeUserId);
        //获取店员信息
        FcmBusiness fcmBusiness2 = fcmBusinessMapper.selectByPrimaryKey(fcmBusinessKey);
        //判断身份是等级
        if (fcmBusiness.getType()>=fcmBusiness2.getType()) {
            //表示没有权限删除
            return 1;
        }
        fcmBusinessMapper.deleteByPrimaryKey(fcmBusinessKey);
        //查看用户还有没有绑定关系的门店，如果没有将用户身份降级
        List<FcmBusiness> list=fcmBusinessMapper.selectBusinessByUserId(storeUserId);
        if (list.size()==0){
            FcmUser fcmUser = fcmUserMapper.selectByPrimaryKey(storeUserId);
            if (fcmUser.getType()==1) {
                //如果用户身份是商家，将用户身份设置为普通用户
                fcmUser.setType(UserIdentityEmun.CODE_0.getCode());
                fcmUserMapper.updateTypeByUserId(fcmUser);
            }
        }
        return 2;
    }

    @Override
    public FcmStore getStoreInfo(FcmBusiness fcmBusiness) {
        return fcmStoreMapper.selectByPrimaryKey(fcmBusiness.getStoreId());
    }

    @Override
    public List<FcmUserFraction> getFractionList(FcmBusiness fcmBusiness,int page) {
        Parameter3 parameter3=new Parameter3();
        parameter3.setVar1(fcmBusiness.getStoreId());
        parameter3.setVar2(page*showRow);
        parameter3.setVar3(showRow);
        List<FcmUserFraction> list= fcmUserFractionMapper.selectFractionByStoreId(parameter3);
        return list;
    }

    @Override
    public List<FcmUserConsumption> getConsumpitionList(FcmBusiness fcmBusiness,int page) {
        Parameter3 parameter3=new Parameter3();
        parameter3.setVar1(fcmBusiness.getStoreId());
        parameter3.setVar2(page*showRow);
        parameter3.setVar3(showRow);
        List<FcmUserConsumption> list=fcmUserConsumptionMapper.selectConsumptionByStoreId(parameter3);
        return list;
    }

    @Override
    public FcmStoreVO userGetStoreInfo(Long storeId) {
        FcmStore fcmStore= fcmStoreMapper.selectByPrimaryKey(storeId);
        FcmStoreVO fcmStoreVO=new FcmStoreVO();
        BeanUtils.copyProperties(fcmStore,fcmStoreVO);
        List<FcmStoreInfo> fcmStoreInfo=fcmStoreInfoMapper.selectCanSeeByStoreId(storeId);
        fcmStoreVO.setStoreInfoList(fcmStoreInfo);
        return fcmStoreVO;
    }

    @Override
    public List<FcmUserRecharges> getStoreRechargesList(FcmBusiness fcmBusiness, int page) {
        Parameter3 parameter3=new Parameter3();
        parameter3.setVar1(fcmBusiness.getStoreId());
        parameter3.setVar2(page*showRow);
        parameter3.setVar3(showRow);
        List<FcmUserRecharges> list=fcmUserRechargesMapper.selectRechargesByStoreId(parameter3);
        return list;
    }

    @Override
    public List<FcmBusinessVO> getStoreUserList(FcmBusiness fcmBusiness, int page) {
        List<FcmBusinessVO> list;
        Parameter3 parameter3=new Parameter3();
        parameter3.setVar1(fcmBusiness.getStoreId());
        parameter3.setVar2(page*showRow);
        parameter3.setVar3(showRow);
        if (fcmBusiness.getType()==0){
            list=fcmBusinessMapper.selectStoreUserByStoreId(parameter3);
        }else if (fcmBusiness.getType()==1){
            list=fcmBusinessMapper.selectStoreUserByStoreId2(parameter3);
        }else {
            list=null;
        }
        return list;
}

    @Override
    public FcmBusinessVO getStoreUserInfo(FcmBusiness fcmBusiness, Long storeUserId) {
        FcmBusinessKey fcmBusinessKey=new FcmBusinessKey();
        fcmBusinessKey.setStoreId(fcmBusiness.getStoreId());
        fcmBusinessKey.setUserId(storeUserId);
        FcmBusinessVO fcmBusinessVO=fcmBusinessMapper.selectBussinessUserInfoByUserId(fcmBusinessKey);
        if (fcmBusinessVO==null||fcmBusiness.getType()>=fcmBusinessVO.getType()){
            return null;
        }
        return fcmBusinessVO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean setStoreOpenOrClose(FcmBusiness fcmBusiness) {
        FcmStore fcmStore = fcmStoreMapper.selectByPrimaryKey(fcmBusiness.getStoreId());
        if (fcmStore.getState()==1){
            fcmStore.setState(StoreStateEmun.CODE_0.getCode());
            fcmStoreMapper.updateStateById(fcmStore);
            return true;
        }
        fcmStore.setState(StoreStateEmun.CODE_1.getCode());
        fcmStoreMapper.updateStateById(fcmStore);
        return false;

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int setStoreIdentity(FcmBusiness fcmBusiness, Long storeUserId, Byte type) {
        FcmBusinessKey fcmBusinessKey = new FcmBusinessKey();
        fcmBusinessKey.setStoreId(fcmBusiness.getStoreId());
        fcmBusinessKey.setUserId(storeUserId);

        //判断添加人是否有权限添加
        FcmBusiness fcmBusiness2 = fcmBusinessMapper.selectByPrimaryKey(fcmBusinessKey);
        if (fcmBusiness.getType()<type){
            //判断用户是否要添加的员工是否存在
            if (fcmBusiness2!=null){
                fcmBusiness2.setType(type);
                fcmBusinessMapper.updateTypeByKey(fcmBusiness2);
                return 3;
            }
            return 2;
        }
        return 1;

    }

    @Override
    public BigDecimal getFractionProportion(FcmBusiness fcmBusiness) {
        FcmFractionProportion fcmFractionProportion = fcmFractionProportionMapper.selectFractionProportionByStoreIdAndZero(fcmBusiness.getStoreId());
        if (fcmFractionProportion==null){
            return new BigDecimal(0.01);
        }
        return fcmFractionProportion.getFractionProportion();
    }

}
